/**
 * 
 */
package com.feib.stms.action;


import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;

import com.feib.stms.model.Group;
import com.feib.stms.model.LabelValue;
import com.feib.stms.model.UploadFile;
import com.feib.stms.model.UploadFileActionLog;
import com.feib.stms.model.User;
import com.feib.stms.pagination.PaginatedList;
import com.feib.stms.service.GroupService;
import com.feib.stms.service.UploadFileActionLogService;
import com.feib.stms.service.UploadFileService;
import com.feib.stms.util.StmsUtil;
import com.feib.stms.util.UserUtil;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.DontBind;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.DateTypeConverter;
import net.sourceforge.stripes.validation.Validate;

/**
 * @title (#)Stms9003ActionBean.java<br>
 * @description 上傳記錄查詢 <br>
 * @author Jeff Gu<br>
 * @version 
 * @copyright Far Eastern International Bank Copyright (c) 2012<br>
 * @2012/05/01 create by Jimmy Liu<br>
 *
 */
public class Stms9003ActionBean extends BaseActionBean 
{
	private static final long serialVersionUID = -3360927795653829350L;

	private static final String DEFAULT_JSP = "/WEB-INF/views/stms9003/stms9003.jsp";
	private static final String DETAIL_JSP = "/WEB-INF/views/stms9003/stms9003Detail.jsp";

	/*畫面sortHeader傳回來的值*/
	private static List<String> SORT_KEYS = new ArrayList<String>();
	static{
		SORT_KEYS.add("uploadDate".toLowerCase());
		SORT_KEYS.add("fileFormat".toLowerCase());
		SORT_KEYS.add("fileName".toLowerCase());
		SORT_KEYS.add("uploadUser".toLowerCase());
	}

	/*使用SORT_KEYS作key值取得對應的資料庫欄位名稱*/
	private static Map<String,String> SORT_FIELD =  new HashMap<String, String>();
	static{
		SORT_FIELD.put("uploadDate".toLowerCase(), "UPLOAD_DATE");
		SORT_FIELD.put("fileFormat".toLowerCase(), "FILE_FORMAT");
		SORT_FIELD.put("FILENAME".toLowerCase(), "FILE_NAME");
		SORT_FIELD.put("uploadUser".toLowerCase(), "UPLOAD_USER_PK");
	}

	/** 
	 * 查詢條件-上傳日期起
	 */
	@Validate( required=true, on={"doQuery"}, converter=DateTypeConverter.class)
	private Date qryUploaddDateFrom;

	/**
	 * 查詢條件-上傳時間起
	 */
	private Integer qryUploadDateHourFrom;

	/**
	 * 查詢條件-上傳日期迄
	 */
	@Validate( required=true, on={"doQuery"}, converter=DateTypeConverter.class)
	private Date qryUploadDateTo;

	/**
	 * 查詢條件-上傳時間迄
	 */
	private Integer qryUploadDateHourTo;

	/**
	 * 查詢條件-上傳經辦ID
	 */
	private String qryUploadUserId;

	/**
	 * 查詢條件-檔案類型
	 */    
	private List<String> qryFileFormats;

	/**
	 * 查詢條件
	 */
	@Validate( required=true, on={"doViewDetail"})
	private Long qryUploadFilePk;

	/**
	 * 查詢結果(多筆)
	 */
	private PaginatedList<UploadFile> queryResult;

	/**
	 * 查詢結果(單筆)
	 */
	private UploadFile viewUploadFile;
	
	/**
	 * 查詢檔案操作紀錄
	 */
	private List<UploadFileActionLog> qryUploadFileActionLog;
	
	
	/**
	 *畫面輸入查詢條件
	 */
	private UploadFile qryUploadFile;
	
	
	/**
	 * 訊息-畫面顯示用
	 */
	protected String message;

	/**
	 * 錯誤訊息-畫面顯示用
	 */
	protected String errorMessage;
	
	
	
	
	
	
	/**
	 * 查詢檔案操作紀錄結果(單筆)
	 */
	private UploadFileActionLog viewUploadFileActionLog;

	@SpringBean("uploadFileService")
	private UploadFileService uploadFileService;
	
	@SpringBean("uploadFileActionLogService")
	private UploadFileActionLogService uploadFileActionLogService;

	@SpringBean("groupService")
	private GroupService groupService;

	

	
	@DontBind
	@DefaultHandler
	public Resolution doDefault()
	{
		logger.debug("Enter doDefault mehtod.");
		

		Calendar cal  = Calendar.getInstance();
		cal.add(Calendar.DAY_OF_MONTH, 1);
		qryUploaddDateFrom = cal.getTime();
		qryUploadDateTo = cal.getTime();		
		qryUploadDateHourFrom = new Integer(0);
		qryUploadDateHourTo = new Integer(24);
		if (!UserUtil.isBankGroup())
		{
			try {
				qryUploadFile = new UploadFile();
				qryUploadFile.setGroup(new Group());
				BeanUtils.copyProperties(UserUtil.getUserGroup(), qryUploadFile.getGroup(), new String[]{"users", "roles"});
			} catch (Exception e) {
			} 
		}
		
		
		return new ForwardResolution(DEFAULT_JSP); 
	}

	/**
	 * 查詢
	 * @return
	 */
	public Resolution doQuery()
	{
		logger.debug("Enter doQuery mehtod.");
		logger.debug("qryUploaddDateFrom={}, qryUploadDateHourFrom={}, qryUploadDateTo={}, qryUploadDateHourTo={}, qryUploadUserId={}, qryFileFormats={} ", new Object[]{qryUploaddDateFrom, qryUploadDateHourFrom,qryUploadDateHourTo,qryUploadDateHourTo, qryUploadUserId, qryFileFormats});

	

		/* 設定換頁、排序等條件*/
		String sortCriterion = "UPLOAD_DATE";
		int sortDirection = 1;
		PaginatedList<UploadFile> qryCondition = new PaginatedList<UploadFile>();
		
		
		
		/* 銀行端一定要輸入群組代號 */
		if (UserUtil.isBankGroup() && "doQuery".equalsIgnoreCase(this.getContext().getEventName()))
		{
			if (null != qryUploadFile && null != qryUploadFile.getGroup() && null != qryUploadFile.getGroup().getGroupNo())
			{
				Group dbGroup = groupService.getByGroupNo(qryUploadFile.getGroup().getGroupNo());
				
				if (null == dbGroup)
				{
					this.errorMessage = "券商代號有誤！";
					return new ForwardResolution(DEFAULT_JSP);
				}
				qryUploadFile.setGroup(dbGroup);
			}
			else
			{
				this.errorMessage = "請輸入券商代號！";
				return new ForwardResolution(DEFAULT_JSP);
			}
		}
		
		
		if (null != queryResult)
		{
			sortCriterion = queryResult.getSortCriterion();
			sortDirection = queryResult.getSortDirection();
			logger.debug(" sortCriterion={}  sortDirection={} ", new Object[]{queryResult.getSortCriterion(), queryResult.getSortDirection()});
			qryCondition.setPageNumber(queryResult.getPageNumber());
			qryCondition.setPageSize(queryResult.getPageSize());
			String StringSortCriterion=SORT_FIELD.get(sortCriterion.toLowerCase());
			qryCondition.setSortCriterion(StringSortCriterion);
			qryCondition.setSortDirection(sortDirection);
			logger.debug(" qryCondition:sortCriterion={}  sortDirection={} ", new Object[]{qryCondition.getSortCriterion(), qryCondition.getSortDirection()});
			
		}
		/*沒有設定排序就以第一欄作排序條件*/
		if (null == queryResult || StringUtils.isEmpty(queryResult.getSortCriterion()) || !SORT_KEYS.contains(queryResult.getSortCriterion().toLowerCase()))
		{
			qryCondition.setSortCriterion(SORT_FIELD.get(SORT_KEYS.get(0)));
		}

		
		/* 設定查詢條件-上傳日期起*/
		Calendar cal = Calendar.getInstance();
		cal.setTime(this.qryUploaddDateFrom);
		cal.set(Calendar.HOUR_OF_DAY, this.qryUploadDateHourFrom);
		this.qryUploaddDateFrom = cal.getTime();

		/* 設定查詢條件-上傳日期迄*/
		cal.setTime(this.qryUploadDateTo);
		cal.set(Calendar.HOUR_OF_DAY, this.qryUploadDateHourTo);
		this.qryUploadDateTo = cal.getTime();

				
		/* 設定user可以查詢的 券商代號*/
		qryUploadFile.setUploadUser(new User());
		qryUploadFile.getUploadUser().setDealSecuritiesNos(UserUtil.getUser().getDealSecuritiesNos());
		if (StringUtils.isNotEmpty(this.qryUploadUserId))
		{
			qryUploadFile.getUploadUser().setUserId(qryUploadUserId);
			
		}

		List<String> fileFormats = new ArrayList<String>();
		if (CollectionUtils.isNotEmpty(this.qryFileFormats))
		{
			for(String qryFileFormat : this.qryFileFormats)
			{
				if (StringUtils.isNotEmpty(qryFileFormat))
					fileFormats.add(qryFileFormat);
			}
		}

		this.queryResult = uploadFileService.findPaginatedListByUploadFile(qryUploadFile, this.qryUploaddDateFrom, this.qryUploadDateTo, null, null, fileFormats, qryCondition);

		if (null == this.queryResult)
			this.queryResult = qryCondition;
		
		queryResult.setSortCriterion(sortCriterion);
		queryResult.setSortDirection(sortDirection);

		if (CollectionUtils.isEmpty(this.queryResult.getPaginatedList()))
			this.message = "查無資料！";

		return new ForwardResolution(DEFAULT_JSP); 
	}

	/**
	 * 查看上傳歷程
	 * @return
	 */
	public Resolution doViewDetail()
	{
		logger.debug("Enter doViewDetail mehtod.");
		logger.debug("qryUploaddDateFrom={}, qryUploadDateHourFrom={}, qryUploadDateTo={}, qryUploadDateHourTo={}, qryUploadUserId={}, qryFileFormats={}, qryUploadFilePk={} ", new Object[]{qryUploaddDateFrom, qryUploadDateHourFrom, qryUploadDateHourTo, qryUploadUserId, qryFileFormats, qryUploadFilePk});

		if (null!=this.qryUploadFilePk)
		{
			qryUploadFile.setUploadUser(new User());
			qryUploadFile.setUploadFilePk(qryUploadFilePk);
			
		}

		

		this.viewUploadFile = uploadFileService.findUploadFileByPk(qryUploadFilePk);
		
		this.setQryUploadFileActionLog(uploadFileActionLogService.findUploadFileActionLog(qryUploadFile));
		
		return new ForwardResolution(DETAIL_JSP); 
	}

	public Collection<LabelValue> getUploadFileFormats()
	{
		return StmsUtil.getInstance().getUploadFileFormats();
	}

	public List<String> getQryFileFormats() {
		return qryFileFormats;
	}

	public void setQryFileFormats(List<String> qryFileFormats) {
		this.qryFileFormats = qryFileFormats;
	}

	public Integer getQryUploadDateHourFrom() {
		return qryUploadDateHourFrom;
	}

	public void setQryUploadDateHourFrom(Integer qryUploadDateHourFrom) {
		this.qryUploadDateHourFrom = qryUploadDateHourFrom;
	}

	public Integer getQryUploadDateHourTo() {
		return qryUploadDateHourTo;
	}

	public void setQryUploadDateHourTo(Integer qryUploadDateHourTo) {
		this.qryUploadDateHourTo = qryUploadDateHourTo;
	}

	public Date getQryUploadDateTo() {
		return qryUploadDateTo;
	}

	public void setQryUploadDateTo(Date qryUploadDateTo) {
		this.qryUploadDateTo = qryUploadDateTo;
	}

	public Date getQryUploaddDateFrom() {
		return qryUploaddDateFrom;
	}

	public void setQryUploaddDateFrom(Date qryUploaddDateFrom) {
		this.qryUploaddDateFrom = qryUploaddDateFrom;
	}

	public String getQryUploadUserId() {
		return qryUploadUserId;
	}

	public void setQryUploadUserId(String qryUploadUserId) {
		this.qryUploadUserId = qryUploadUserId;
	}

	public PaginatedList<UploadFile> getQueryResult() {
		return queryResult;
	}

	public void setQueryResult(PaginatedList<UploadFile> queryResult) {
		this.queryResult = queryResult;
	}

	public Long getQryUploadFilePk() {
		return qryUploadFilePk;
	}

	public void setQryUploadFilePk(Long qryUploadFilePk) {
		this.qryUploadFilePk = qryUploadFilePk;
	}

	public UploadFile getViewUploadFile() {
		return viewUploadFile;
	}

	public void setViewUploadFile(UploadFile viewUploadFile) {
		this.viewUploadFile = viewUploadFile;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * @param viewUploadFileActionLog 的設定的 viewUploadFileActionLog
	 */
	public void setViewUploadFileActionLog(UploadFileActionLog viewUploadFileActionLog) {
		this.viewUploadFileActionLog = viewUploadFileActionLog;
	}

	/**
	 * @return viewUploadFileActionLog
	 */
	public UploadFileActionLog getViewUploadFileActionLog() {
		return viewUploadFileActionLog;
	}

	/**
	 * @param qryUploadFileActionLog 的設定的 qryUploadFileActionLog
	 */
	public void setQryUploadFileActionLog(List<UploadFileActionLog> qryUploadFileActionLog) {
		this.qryUploadFileActionLog = qryUploadFileActionLog;
	}

	/**
	 * @return qryUploadFileActionLog
	 */
	public List<UploadFileActionLog> getQryUploadFileActionLog() {
		return qryUploadFileActionLog;
	}

	/**
	 * @param qryUploadFile 的設定的 qryUploadFile
	 */
	public void setQryUploadFile(UploadFile qryUploadFile) {
		this.qryUploadFile = qryUploadFile;
	}

	/**
	 * @return qryUploadFile
	 */
	public UploadFile getQryUploadFile() {
		return qryUploadFile;
	}
	
	public String getErrorMessage() {
		return errorMessage;
	}

}
